package com.mxnet.chongjin.nci;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mxnet.chongjin.nci.activity.LoginActivity;
import com.mxnet.chongjin.nci.base.BaseActivity;
import com.mxnet.chongjin.nci.config.Api;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;

import okhttp3.Call;

public class MainActivity extends BaseActivity {
    private Context mContext;
    private Button mButton, mButton2, mButtonTestUrl, mButtonCopy;
    private TextView mTextView1, mTextView2, mTextViewIntent;
    private EditText mEditText;
    private boolean isGetMac;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    private boolean isTest = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = MainActivity.this;
        mPreferences = getPreferences();
        mEditor = getEditor();
        isGetMac = mPreferences.getBoolean("isGetMac", false);

        if (isGetMac) {
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            finish();
            // 测试
        }

        mTextViewIntent = findViewById(R.id.textview_main);

        mButtonCopy = findViewById(R.id.btn_main_copy);
        mButtonCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(mTextViewIntent.getText());
                Toast.makeText(mContext, "复制成功！", Toast.LENGTH_SHORT).show();
            }
        });

        mEditText = findViewById(R.id.edittext_main);

        mButtonTestUrl = findViewById(R.id.btn_main_testurl);
        mButtonTestUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEditText.getText())) {
                    Toast.makeText(mContext, "请输入服务器地址！", Toast.LENGTH_SHORT).show();
                } else {
                    OkHttpUtils.get()
                            .url(mEditText.getText().toString() + Api.TEST_URL)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("******==onError==", e.getMessage());
                                    Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                isTest = true;
                                                mEditor.putString("URL_HEADER", mEditText.getText().toString()).commit();
                                                Toast.makeText(mContext, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        Toast.makeText(mContext, response, Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            }
        });

        mTextView1 = findViewById(R.id.textview1);
        // 获取mac，设置给mTextView1
        mTextView1.setText(getSerialNumber());

        mTextView2 = findViewById(R.id.textview2);

        mButton2 = findViewById(R.id.textview3);
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTest) {
                    Toast.makeText(mContext, getSerialNumber(), Toast.LENGTH_SHORT).show();
                    // 获取编号
                    OkHttpUtils.get()
                            .url(mPreferences.getString("URL_HEADER", "") + Api.GET_MACHINE_NO)
                            .addParams("mac", getSerialNumber())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.i("***==GET_MACHINE_NO==", e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    if (!TextUtils.isEmpty(response)) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            boolean isSuccess = jsonObject.optBoolean("success");
                                            if (isSuccess) {
                                                JSONObject data = jsonObject.optJSONObject("data");
                                                String deviceNo = data.optString("device_no");
                                                mEditor.putString("deviceNo", deviceNo).commit();
                                                mTextView2.setText(deviceNo);
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            });
                } else {
                    Toast.makeText(mContext, "请先完成服务器地址测试！", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mButton = findViewById(R.id.btn_main);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTest) {
                    mEditor.putBoolean("isGetMac", true).commit();
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(mContext, "请先完成服务器地址测试！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * getSerialNumber
     *
     * @return result is same to getSerialNumber1()
     */
    public static String getSerialNumber() {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }
}
